package com.example.yichuangyun.utils;

import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spannable;
import android.text.style.ImageSpan;
import android.util.Log;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

public class UrlImageGetter implements Html.ImageGetter {
    private final TextView textView;

    public UrlImageGetter(TextView textView) {
        this.textView = textView;
    }

    @Override
    public Drawable getDrawable(String source) {
        // 使用 Glide 异步加载图片
        Glide.with(textView.getContext())
                .load(source)
                .into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        // 关键步骤：设置图片边界（必须调用！）
                        int width = 50;  // 单位：px（根据需要调整）
                        int height = 50;
                        resource.setBounds(0, 0, width, height);

                        Log.e("@@@@@@@@@", source);
                        // 获取当前 Spannable 并替换占位 Drawable
                        CharSequence text = textView.getText();
                        if (text instanceof Spannable) {
                            Spannable spannable = (Spannable) text;
                            // 找到所有 ImageSpan 并替换为真实图片
                            ImageSpan[] spans = spannable.getSpans(0, text.length(), ImageSpan.class);
                            for (ImageSpan span : spans) {
                                if (span.getSource().equals(source)) {
                                    int start = spannable.getSpanStart(span);
                                    int end = spannable.getSpanEnd(span);
                                    spannable.removeSpan(span);
                                    spannable.setSpan(new ImageSpan(resource), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                                }
                            }
                        }
                        textView.setText(text); // 刷新 TextView
                    }

                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                        // 加载失败时显示错误占位图
                        if (errorDrawable != null) {
                            errorDrawable.setBounds(0, 0, 50, 50);
                        }
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                    }
                });

        // 返回一个临时占位 Drawable（必须设置边界！）
        Drawable placeholder = textView.getContext().getResources().getDrawable(android.R.drawable.ic_menu_gallery);
        placeholder.setBounds(0, 0, 50, 50);
        return placeholder;
    }
}
